(function () {
	var serverSelectFuData = {};
    var serverSelectFuTable;
	// 获取合同列表页面数据 服务器分页
    var serverData = {};
    var serverTable;//合同列表
    var serverCruxDateData = {};
    var serverCruxDateTable;//合同关键点列表
    var serverThreeData = {};
    var serverThreeTable;
    
    var Contract;
    var orderArea;
    var dutyOfficer;
    var industryInfo;
    var totalSum;
    var cProductProcess;
    var state;
    var contractType;
    var nameDate;
    var createTimeStartDate;
    var createTimeEndDate;
    var signedDateStartDate;
    var signedDateEndDate;
    var scheduleDateStartDate;
    var scheduleDateEndDate;
    var urlsum = window.rootPath+ '/ui/contractviewindex/getPageSum?type=1';
    jQuery(document).ready(function () {
    	
    	initQueryParam();
    	$("#orderArea").multiselect({
		      header: true,
		      height: 175,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
    	
    	$("#generateState").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
    	
    	$("#totalSum").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
    	
    	$("#industryInfo").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
    	
    	$("#contractState").multiselect({
    		header: true,
    		height: 250,
    		minWidth: 150,
    		classes: '',
    		checkAllText: '选中全部',
    		uncheckAllText: '取消全选',
    		noneSelectedText: '请勾选',
    		selectedText: '# 选中',
    		selectedList: 5,
    		show: null,
    		hide: null,
    		autoOpen: false,
    		multiple: true,
    		position: {},
    		appendTo: "body",
    		menuWidth:null
    	});
    	
    	$("#contractType").multiselect({
    		header: true,
    		height: 250,
    		minWidth: 150,
    		classes: '',
    		checkAllText: '选中全部',
    		uncheckAllText: '取消全选',
    		noneSelectedText: '请勾选',
    		selectedText: '# 选中',
    		selectedList: 5,
    		show: null,
    		hide: null,
    		autoOpen: false,
    		multiple: true,
    		position: {},
    		appendTo: "body",
    		menuWidth:null
    	});
    	
    	if(jQuery("#orderArea").val()) {
    		serverData.orderArea=jQuery("#orderArea").val().toString();
    		orderArea = jQuery("#orderArea").val().toString();
    		urlsum += "&orderArea=" + orderArea;
    	}
    	if(jQuery("#dutyOfficer").val()) {
    		serverData.dutyOfficer=jQuery("#dutyOfficer").val();
    		dutyOfficer=jQuery("#dutyOfficer").val();
    		urlsum += "&dutyOfficer=" + dutyOfficer;
    	}
    	if(jQuery("#industryInfo").val()) {
    		serverData.industryInfo=jQuery("#industryInfo").val().toString();
    		industryInfo=jQuery("#industryInfo").val().toString();
    		urlsum += "&industryInfo=" + industryInfo;
    	}
    	if(jQuery("#totalSum").val()) {
    		serverData.totalSum=jQuery("#totalSum").val().toString();
    		totalSum=jQuery("#totalSum").val().toString();
    		urlsum += "&totalSum=" + totalSum;
    	}
    	if(jQuery("#generateState").val()) {
    		serverData.cProductProcess=jQuery("#generateState").val().toString();
    		cProductProcess=jQuery("#generateState").val().toString();
    		urlsum += "&cProductProcess=" + cProductProcess;
    	}
    	if(jQuery("#contractState").val()) {
    		serverData.state=jQuery("#contractState").val().toString();
    		state=jQuery("#contractState").val().toString();
    		urlsum += "&state=" + state;
    	}
    	if(jQuery("#contractType").val()) {
    		serverData.contractType=jQuery("#contractType").val().toString();
    		contractType=jQuery("#contractType").val().toString();
    		urlsum += "&contractType=" + contractType;
    	}
    	var nameDate = jQuery("#nameDate").val();
    	if(nameDate=="createTime"){
    		if(jQuery("#startDate").val()) {
    			serverData.createTimeStartDate=jQuery("#startDate").val();
    			createTimeStartDate=jQuery("#startDate").val();
    			urlsum += "&createTimeStartDate=" + createTimeStartDate;
    		}
    		if(jQuery("#endDate").val()) {
    			serverData.createTimeEndDate=jQuery("#endDate").val();
    			createTimeEndDate=jQuery("#endDate").val();
    			urlsum += "&createTimeEndDate=" + createTimeEndDate;
    		}
    	}
    	if(nameDate=="signedDate"){
    		if(jQuery("#startDate").val()) {
    			serverData.signedDateStartDate=jQuery("#startDate").val();
    			signedDateStartDate=jQuery("#startDate").val();
    			urlsum += "&signedDateStartDate=" + signedDateStartDate;
    		}
    		if(jQuery("#endDate").val()) {
    			serverData.signedDateEndDate=jQuery("#endDate").val();
    			signedDateEndDate=jQuery("#endDate").val();
    			urlsum += "&signedDateEndDate=" + signedDateEndDate;
    		}
    	}
    	if(nameDate=="scheduleDate"){
    		if(jQuery("#startDate").val()) {
    			serverData.scheduleDateStartDate=jQuery("#startDate").val();
    			scheduleDateStartDate=jQuery("#startDate").val();
    			urlsum += "&scheduleDateStartDate=" + scheduleDateStartDate;
    		}
    		if(jQuery("#endDate").val()) {
    			serverData.scheduleDateEndDate=jQuery("#endDate").val();
    			scheduleDateEndDate=jQuery("#endDate").val();
    			urlsum += "&scheduleDateEndDate=" + scheduleDateEndDate;
    		}
    	}
    	getSum(urlsum);
    	serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/contractviewindex/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                	data: null
                },
                /*{
    	            data: null,
    	            defaultContent: "",
    	            render: function (data, type, full, meta) {
    	            	var id=data.id
    	            	var operate ='<input type="checkbox" class="checkchildContract">';
    	            	return operate;
    	            }   
                },*/
                {
                    data: 'supplierCode'
                },
                {
                	data: 'contractType'
                },
                
                {
                    data: 'industryInfo'
                },
                {
                    data: 'totalSum'
                },
                {
                    data: 'orderUnit',
	                render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return '<font title=" ">'+'</font>';
						}else{
							return '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>';
						}
					}
                },
                {
                    data: 'orderArea'
                },
                {
                    data: 'sczt'
                },
                {
                    data: 'signedDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'scheduleDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                /*{
                    data: 'giveOutDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'deliveryDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'debugDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'acceptanceDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'stop'
                },
                {
                    data: 'freightCharge'
                },*/
                {
                    data: 'projectName'
                },
                {
                	data: 'state'
                }
                /*{
                    data: 'orderAddress'
                },
                {
                    data: 'orderDelegate'
                },
                {
                    data: 'orderPhone'
                },
                {
                    data: 'orderZip'
                },
                {
                    data: 'postUnit'
                },
                {
                    data: 'postAddress'
                },
                {
                    data: 'postDelegate'
                },
                {
                    data: 'postPhone'
                },
                {
                    data: 'postZip'
                }*/
                ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
    	
    	serverTable.on('dblclick','tr',function(event) {
        	event = event|| window.event;
        	var target = event.target|| event.scrElement;
        	var data = serverTable.row(this).data();
        	var id=data.contractId;
 		   	var type=data.contractType;
 		   	var url=window.rootPath+'/ui/contractviewindex/getcontractview/' + id +'/'+ type;
 		   	window.location.href = url;
        	return;
        });
    	
    	/*// 查看合同
    	serverTable.on('dblclick',function() {
    		var selects=getSelect("#dataTable",1);
 		   if(selects.length>1){
 			   $.springLemonDialog({
 			         content: '只能查看一条记录',// 确认文本
 			         clickButton: function (sender, modal, index) {// 按钮事件处理
 			             $(this).closeDialog(modal);
 			         }
 			   }); 
 		   }
 		   else if(selects.length==0){
 			   $.springLemonDialog({
 			         content: '请选择要查看的记录',// 确认文本
 			         clickButton: function (sender, modal, index) {// 按钮事件处理
 			             $(this).closeDialog(modal);
 			         }
 			   }); 
 		   }else if(selects.length==1){
		   var taskId="0";
		   var id=selects[0].contractId;
		   var type=selects[0].contractType;
		   var url=window.rootPath+'/ui/contractviewindex/getcontractview/' + id +'/'+ type;
		   window.location.href = url;
    	});*/
    	
    	
    	// 查询
        jQuery('.searchBtn').on('click',function(){
        	clearQueryServerData();
        	getServerDate();
        	serverTable.draw();
        	getSum(urlsum);
        	//clearQueryServerData();
        })
        
        var getServerDate=function(){
        	if(jQuery("#Contract").val()){
        		serverData.Contract=jQuery("#Contract").val();
        		Contract = jQuery("#Contract").val().toString();
        		urlsum += "&Contract=" + Contract;
        	}
        	if(jQuery("#orderArea").val()) {
        		serverData.orderArea=jQuery("#orderArea").val().toString();
        		orderArea = jQuery("#orderArea").val().toString();
        		urlsum += "&orderArea=" + orderArea;
        	}
        	if(jQuery("#dutyOfficer").val()) {
        		serverData.dutyOfficer=jQuery("#dutyOfficer").val();
        		dutyOfficer=jQuery("#dutyOfficer").val();
        		urlsum += "&dutyOfficer=" + dutyOfficer;
        	}
        	if(jQuery("#industryInfo").val()) {
        		serverData.industryInfo=jQuery("#industryInfo").val().toString();
        		industryInfo=jQuery("#industryInfo").val().toString();
        		urlsum += "&industryInfo=" + industryInfo;
        	}
        	if(jQuery("#totalSum").val()) {
        		serverData.totalSum=jQuery("#totalSum").val().toString();
        		totalSum=jQuery("#totalSum").val().toString();
        		urlsum += "&totalSum=" + totalSum;
        	}
        	if(jQuery("#generateState").val()) {
        		serverData.cProductProcess=jQuery("#generateState").val().toString();
        		cProductProcess=jQuery("#generateState").val().toString();
        		urlsum += "&cProductProcess=" + cProductProcess;
        	}
        	if(jQuery("#contractState").val()) {
        		serverData.state=jQuery("#contractState").val().toString();
        		state=jQuery("#contractState").val().toString();
        		urlsum += "&state=" + state;
        	}
        	if(jQuery("#contractType").val()) {
        		serverData.contractType=jQuery("#contractType").val().toString();
        		contractType=jQuery("#contractType").val().toString();
        		urlsum += "&contractType=" + contractType;
        	}
        	var nameDate = jQuery("#nameDate").val();
        	if(nameDate=="createTime"){
        		if(jQuery("#startDate").val()) {
        			serverData.createTimeStartDate=jQuery("#startDate").val();
        			createTimeStartDate=jQuery("#startDate").val();
        			urlsum += "&createTimeStartDate=" + createTimeStartDate;
        		}
        		if(jQuery("#endDate").val()) {
        			serverData.createTimeEndDate=jQuery("#endDate").val();
        			createTimeEndDate=jQuery("#endDate").val();
        			urlsum += "&createTimeEndDate=" + createTimeEndDate;
        		}
        	}
        	if(nameDate=="signedDate"){
        		if(jQuery("#startDate").val()) {
        			serverData.signedDateStartDate=jQuery("#startDate").val();
        			signedDateStartDate=jQuery("#startDate").val();
        			urlsum += "&signedDateStartDate=" + signedDateStartDate;
        		}
        		if(jQuery("#endDate").val()) {
        			serverData.signedDateEndDate=jQuery("#endDate").val();
        			signedDateEndDate=jQuery("#endDate").val();
        			urlsum += "&signedDateEndDate=" + signedDateEndDate;
        		}
        	}
        	if(nameDate=="scheduleDate"){
        		if(jQuery("#startDate").val()) {
        			serverData.scheduleDateStartDate=jQuery("#startDate").val();
        			scheduleDateStartDate=jQuery("#startDate").val();
        			urlsum += "&scheduleDateStartDate=" + scheduleDateStartDate;
        		}
        		if(jQuery("#endDate").val()) {
        			serverData.scheduleDateEndDate=jQuery("#endDate").val();
        			scheduleDateEndDate=jQuery("#endDate").val();
        			urlsum += "&scheduleDateEndDate=" + scheduleDateEndDate;
        		}
        	}
        }
        
        var clearQueryServerData=function(){
        	if(serverData.Contract) {
        		serverData.Contract=null;
        		Contract=null;
        	}
        	if(serverData.orderArea) {
        		serverData.orderArea=null;
        		orderArea=null;
        	}
        	if(serverData.dutyOfficer) {
        		serverData.dutyOfficer=null;
        		dutyOfficer=null;
        	}
        	if(serverData.industryInfo) {
        		serverData.industryInfo=null;
        		industryInfo=null;
        	}
        	if(serverData.totalSum) {
        		serverData.totalSum=null;
        		totalSum=null;
        	}
        	if(serverData.cProductProcess) {
        		serverData.cProductProcess=null;
        		cProductProcess=null;
        	}
        	if(serverData.state) {
        		serverData.state=null;
        		state=null;
        	}
        	if(serverData.contractType) {
        		serverData.contractType=null;
        		contractType=null;
        	}
        	
        	if(serverData.createTimeStartDate) {
        		serverData.createTimeStartDate=null;
        		createTimeStartDate=null;
        	}
        	if(serverData.createTimeEndDate) {
        		serverData.createTimeEndDate=null;
        		createTimeEndDate=null;
        	}
        	
        	if(serverData.signedDateStartDate) {
        		serverData.signedDateStartDate=null;
        		signedDateStartDate=null;
        	}
        	if(serverData.signedDateEndDate) {
        		serverData.signedDateEndDate=null;
        		signedDateEndDate=null;
        	}
        	
        	if(serverData.scheduleDateStartDate) {
        		serverData.scheduleDateStartDate=null;
        		scheduleDateStartDate=null;
        	}
        	if(serverData.scheduleDateEndDate) {
        		serverData.scheduleDateEndDate=null;
        		scheduleDateEndDate=null;
        	}
        	
        	urlsum = window.rootPath+ '/ui/contractviewindex/getPageSum?type=1';
        	
        }
    });
	
})();

//初始化查询条件
function initQueryParam(){
	if($('#endDate').val()== "") 
	$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+new Date().getDate());
	if($('#startDate').val() == "")
	$('#startDate').val(new Date().getFullYear()+"-01-01");
}

function getSum(urlsum){
	$.ajax({
        type: "GET", 
        dataType: 'json',
        async: false, 
        url: encodeURI(urlsum),
        success: function(data) {
        	jQuery('#sum').val(data.sum);
        }
	});
}